import useCopy from '@/hooks/web/useCopy'
const useClipboard = initInfo => {
  const { success } = useMessage()

  const { copy } = useCopy()
  const { clipboardRef, copiedRef } = copy()

  // 复制链接
  const copyLink = v => {
    clipboardRef.value = v
    if (unref(copiedRef)) {
      success('链接复制成功！')
    }
  }

  // 复制链接 （选中后的复制链接）
  const copyLinkMenuClick = () => {
    initInfo.value.sourceList.forEach(item => {
      if (item.active) {
        copyLink(item.url)
      }
    })
  }
  // 复制代码
  const copyHtml = v => {
    let res = ''
    if (v.fileType === 'video') {
      res =
        '<video src="' +
        v.url +
        '" controls poster="' +
        (v.attachment_id ? v.attachment.url : '') +
        '" preload="auto" webkit-playsinline="true" playsinline="true" x-webkit-airplay="allow" x5-video-player-type="h5" x5-video-player-fullscreen="true" x5-video-orientation="portraint" style="object-fit:fill"></video>'
    } else if (v.fileType === 'audio') {
      res = '<audio src="' + v.url + '" controls preload="auto"></audio>'
    } else if (v.fileType === 'image') {
      res = '<img src="' + v.url + '" alt="' + (v.alt ? v.alt : v.title) + '" />'
    } else {
      res = '<a target="blank" href="' + v.url + '" title="' + v.title + '" download>' + v.title + '</a>'
    }
    clipboardRef.value = res
    if (unref(copiedRef)) {
      console.log(v, '复制代码')
      success('代码复制成功！')
    }
  }

  // 复制代码 （选中后的复制代码）
  const copyHtmlMenuClick = () => {
    initInfo.value.sourceList.forEach(item => {
      if (item.active) {
        copyHtml(item)
      }
    })
  }

  return {
    copyLink,
    copyLinkMenuClick,
    copyHtml,
    copyHtmlMenuClick
  }
}
export default useClipboard
